package be.idamf.cortizone.events
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;

	public class Router
	{
		private static const MAP:Dictionary = new Dictionary();
		
		public static function map(target:Object, type:String, kind:Class = null, originator:Class = null):void
		{
			var key:String;
			
			if (kind)
				key = getQualifiedClassName(kind) + ":" + type;
			else
				key = type;
			
			MAP[key] = new Dictionary(true);
			MAP[key][target] = (originator ? getQualifiedClassName(originator) : "");
		}
		
		public static function handle(event:*):Boolean
		{
			var target:Object;
			var key:String;
			var type:String;
			var hasHandler:Boolean;
			
			type = (event is Event ? event.type : event.toString());
			
			key = type;
			
			if (!MAP[key])
				key = getQualifiedClassName(event) + ":" + type;
			
			if (MAP[key])
			{
				for (target in MAP[key])
				{
					if (MAP[key][target] == "" || MAP[key][target] == getQualifiedClassName(event.target))
					{
						target[type + "Handler"](event);
						hasHandler = true;
					}
				}
			}
			
			return hasHandler;
		}
		
		/**
		 * @private
		 */
		public function Router()
		{
			throw new Error("Static Class");
		}
	}
}